package com.nnfe.fyd.feature.main;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.nnfe.fyd.R;
import com.nnfe.fyd.ShareKey;
import com.nnfe.fyd.base.AdGallery;
import com.nnfe.fyd.base.WebViewActivity;
import com.nnfe.fyd.bean.HttpResult;
import com.nnfe.fyd.bean.ad.AdBean;
import com.nnfe.fyd.feature.inquiryPrice.AutoInquiryPriceActivity;
import com.nnfe.fyd.feature.login.LoginActivity;
import com.nnfe.fyd.http.CallbackImpl;
import com.nnfe.fyd.utils.SharePrefUtil;
import com.nnfe.fyd.utils.ToastUtil;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Response;

/**
 * ----------------------------------
 * Created by xyd on 2017/3/15.
 * <p/>
 * 描  述： 首页 - 首页
 * <p/>
 * 修订历史:
 * ----------------------------------
 */

public class MainFragmentTabOne extends MainFragmentBase {
    @Bind(R.id.btn_inquiry)
    Button btnInquiry;
    @Bind(R.id.banner)
    AdGallery banner;
    @Bind(R.id.ovalLayout)
    LinearLayout ovalLayout;
    @Bind(R.id.bd)
    RelativeLayout bd;

    @Override
    public Object getTitleString() {
        return null;
    }

    @Override
    public int getContentView() {
        return R.layout.fragment_main_tab_one;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        btnInquiry.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                return true;
            }
        });

        return rootView;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick(R.id.btn_inquiry)
    public void onClick() {
        if (!SharePrefUtil.getBoolean(mContext, ShareKey.isLogin, false)) {
            LoginActivity.lauch(mContext, LoginActivity.TYPE_LOGIN);
        } else {
            checkToken();
        }
    }


    /**
     * 验证token是否有效
     */
    private void checkToken() {
        Call<HttpResult<Boolean>> call = service.checkToken(SharePrefUtil.getString(mContext, ShareKey.UserId, "")
        ,SharePrefUtil.getString(mContext,ShareKey.Token,""));
        call.enqueue(new CallbackImpl<Boolean>() {
            @Override
            public void responseSuccess(Call<HttpResult<Boolean>> call, Response<HttpResult<Boolean>> response) {
                if (response.body().getStatus() == 0){
                    if (response.body().getData()){
                        startActivity(new Intent(mContext, AutoInquiryPriceActivity.class));
                    }else {
                        ToastUtil.showToast("登录失效，请重新登录");
                        SharePrefUtil.clear(mContext);
                        LoginActivity.lauch(mContext,LoginActivity.TYPE_LOGIN);
                    }
                }
            }
        });
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }

    @Override
    public void onResume() {
        super.onResume();
        donetAD();
    }

    private List<AdBean> list;
    private void donetAD(){
        if(list==null||list.size()==0) {
            service.findByType("1").enqueue(new CallbackImpl<List<AdBean>>() {
                @Override
                public void responseSuccess(Call<HttpResult<List<AdBean>>> call, Response<HttpResult<List<AdBean>>> response) {
                    list = response.body().data;
                    banner.setVisibility(View.VISIBLE);
                    ovalLayout.setVisibility(View.VISIBLE);
                    String[] mris = new String[list.size()];
                    for (int i = 0; i < list.size(); i++) {
                        mris[i] = list.get(i).imgUrl;
                    }
                    // 第二和第三参数 2选1 ,参数2为 图片网络路径数组 ,参数3为图片id的数组,本地测试用 ,2个参数都有优先采用 参数2
                    banner.start(mContext, mris, 4000, ovalLayout, R.drawable.dot_focused,
                            R.drawable.dot_normal);
                    banner.setMyOnItemClickListener(new AdGallery.MyOnItemClickListener() {
                        @Override
                        public void onItemClick(int curIndex) {
                            WebViewActivity.lauch(mContext,list.get(curIndex).title+"",list.get(curIndex).jumpUrl);
                        }
                    });
                }

            });
        }
    }










}
